package com.quark.controller.system;

import com.quark.annotation.SysLog;
import com.quark.model.system.menu.MenuTreeVO;
import com.quark.model.system.menu.SysMenu;
import com.quark.model.vo.sys.MenuVO;
import com.quark.model.vo.sys.PermissionAssignment;
import com.quark.result.RequestResult;
import com.quark.service.system.MenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author 张高昌
 * @version 版本
 * @date 2021/11/8 14:51
 * @description: 功能描述
 */
@Api(tags = "菜单管理")
@RestController
@RequiredArgsConstructor
@RequestMapping("/menu")
public class MenuController {

    private final MenuService menuService;

    @ApiOperation("登录获取菜单")
    @GetMapping("getMoveRouter")
    public RequestResult getMoveRouter() {
        List<MenuTreeVO> moveRouter = menuService.getMoveRouter();

        return RequestResult.success(moveRouter);
    }

    @ApiOperation("获取菜单列表")
    @GetMapping("menuList")
    public RequestResult menuList(Integer parentId) {
        if (parentId == null) {
            parentId = 0;
        }

        return RequestResult.success(menuService.getMenuPageList(parentId));
    }

    @ApiOperation("角色权限分配")
    @GetMapping("permissionAssignment")
    public RequestResult<List<PermissionAssignment>> permissionAssignment() {

        return RequestResult.success(menuService.getPermissionAssignment());
    }

    @ApiOperation("新增菜单")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('sys_menu_add')")
    public RequestResult menuAdd(@RequestBody @Valid MenuVO vo) {

        return RequestResult.success(menuService.menuAdd(vo));
    }

    @ApiOperation("修改菜单")
    @SysLog("修改菜单")
    @PostMapping("updateMenuInfo")
    @PreAuthorize("@pms.hasPermission('sys_menu_update')")
    public RequestResult updateMenuInfo(@RequestBody @Valid SysMenu menu) {

        return RequestResult.success(menuService.updateMenu(menu));
    }

    @ApiOperation("修改状态")
    @GetMapping("updateStatus/{id}/{status}")
    public RequestResult updateStatus(@PathVariable("id") String id, @PathVariable("status") String status) {

        return RequestResult.success(menuService.getMenuStatus(id, status));
    }

    @ApiOperation("删除菜单")
    @GetMapping("deleteMenu/{id}")
    @PreAuthorize("@pms.hasPermission('sys_menu_delete')")
    public RequestResult deleteMenu(@PathVariable("id") String id) {

        return RequestResult.success(menuService.deleteMenuById(Integer.valueOf(id)));
    }

}
